package com.seuic.hnapps.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * getInstance: 获取SP实例
 * put        : SP中写入数据
 * getString  : SP中读取String
 * getInt     : SP中读取int
 * getLong    : SP中读取long
 * getFloat   : SP中读取float
 * getBoolean : SP中读取boolean
 * getAll     : SP中获取所有键值对
 * remove     : SP中移除该key
 * contains   : SP中是否存在该key
 * clear      : SP中清除所有数据
 */
public class SpUtils {
    private static SharedPreferences preferences;

    private static SharedPreferences.Editor editor;

    private static SpUtils spUtil;

    /**
     *      * 保存在手机里面的名字
     *     
     */

    public static String FILE_NAME = "JiLu";


    public static SpUtils getSPUtil(Context context) {
        if (spUtil == null) {
            synchronized (SpUtils.class) {
                spUtil = new SpUtils();
                preferences = context.getSharedPreferences(FILE_NAME, context.MODE_MULTI_PROCESS);
                editor = preferences.edit();
            }
        }
        return spUtil;
    }


    public void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }


    public void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.commit();
    }


    public void putFloat(String key, float value) {
        editor.putFloat(key, value);
        editor.commit();
    }


    public void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }


    public void putLong(String key, long value) {
        editor.putLong(key, value);
        editor.commit();
    }

    /**
     *      * 清空SP里所以数据
     *     
     */


    public void clear() {
        editor.clear();
        editor.commit();
    }

    /**
     *      * 删除SP里指定key对应的数据项
     *      * @param key
     *     
     */


    public void remove(String key) {
        editor.remove(key);
        editor.commit();
    }

    /**
     *      * 获取SP数据里指定key对应的value。如果key不存在，则返回默认值defValue。
     *      * @param key
     *      * @param defValue
     *      * @return
     *     
     */


    public String getString(String key, String defValue) {
        return preferences.getString(key, defValue);
    }


    public boolean getBoolean(String key, boolean defValue) {
        return preferences.getBoolean(key, defValue);
    }


    public float getFloat(String key, float defValue) {
        return preferences.getFloat(key, defValue);
    }


    public int getInt(String key, int defValue) {
        return preferences.getInt(key, defValue);
    }


    public long getLong(String key, long defValue) {
        return preferences.getLong(key, defValue);
    }

    /**
     *      * 判断SP是否包含特定key的数据
     *      * @param key
     *      * @return
     *     
     */

    public boolean contains(String key) {
        return preferences.contains(key);
    }

}


